//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.wsd.smartcarwasher.domain.PayInfo;
import com.wsd.smartcarwasher.domain.Refundnfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import java.io.Writer;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
import java.util.*;
import java.util.Map.Entry;

public class CommonUtil {
    private static final String[] hexDigits = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
    private static XStream xstream = new XStream(new XppDriver() {
        public HierarchicalStreamWriter createWriter(Writer out) {
            return new PrettyPrintWriter(out) {
                boolean cdata = true;


                public void startNode(String name, Class clazz) {
                    super.startNode(name, clazz);
                }


                protected void writeText(QuickWriter writer, String text) {
                    if (this.cdata) {
                        writer.write("<![CDATA[");
                        writer.write(text);
                        writer.write("]]>");
                    } else {
                        writer.write(text);
                    }

                }
            };
        }
    });

    public CommonUtil() {
    }

    public static String getRandomOrderId() {
        Random random = new Random(System.currentTimeMillis());

        int value;
        for(value = random.nextInt(); value < 0; value = random.nextInt()) {
        }

        return value + "";
    }

    public static String payInfoToXML(PayInfo pi) {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        Field[] var2 = pi.getClass().getDeclaredFields();
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            Field field = var2[var4];
            field.setAccessible(true);
            String fieldName = field.getName();

            String value = null;
            try {

                    value = field.get(pi).toString();

               
            } catch (IllegalAccessException var9) {
                continue;
            }

            sb.append("<").append(fieldName).append(">").append(value).append("</").append(fieldName).append(">");
        }

        sb.append("</xml>");
        return sb.toString().trim();
    }


    public static String refundInfoToXML(Refundnfo pi) {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        Field[] var2 = pi.getClass().getDeclaredFields();
        int var3 = var2.length;
        for(int var4 = 0; var4 < var3; ++var4) {
            Field field = var2[var4];
            field.setAccessible(true);
            String fieldName = field.getName();
            String value = null;
            try {
                if(!StringUtil.isNullOrEmpty(field.get(pi)))
                {

                    value = field.get(pi).toString();
                }else{
                    System.out.println("缺少参数"+pi);
                }
            } catch (IllegalAccessException var9) {
                continue;
            }

            sb.append("<").append(fieldName).append(">").append(value).append("</").append(fieldName).append(">");
        }

        sb.append("</xml>");
        return sb.toString().trim();
    }




    public static Map<String, String> parseXml(String xml) throws Exception {
        Map<String, String> map = new HashMap();
        Document document = DocumentHelper.parseText(xml);
        Element root = document.getRootElement();
        List<Element> elementList = root.elements();
        Iterator var5 = elementList.iterator();

        while(var5.hasNext()) {
            Element e = (Element)var5.next();
            map.put(e.getName(), e.getText());
        }

        return map;
    }

    public static String mapToXML(Map<String, String> param) {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        Iterator var2 = param.entrySet().iterator();

        while(var2.hasNext()) {
            Entry<String, String> entry = (Entry)var2.next();
            sb.append("<").append((String)entry.getKey()).append(">").append((String)entry.getValue()).append("</").append((String)entry.getKey()).append(">");
        }

        sb.append("</xml>");
        return sb.toString().trim();
    }

    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            int index = ip.indexOf(",");
            return index != -1 ? ip.substring(0, index) : ip;
        } else {
            ip = request.getHeader("X-Real-IP");
            return StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip) ? ip : request.getRemoteAddr();
        }
    }

    public static String getMD5(String str) throws Exception {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return byteArrayToHexString(md.digest(str.getBytes(StandardCharsets.UTF_8)));
        } catch (Exception var2) {
            throw new Exception("MD5加密出现错误");
        }
    }

    private static String byteArrayToHexString(byte[] array) {
        StringBuilder resultSb = new StringBuilder();
        byte[] var2 = array;
        int var3 = array.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            byte b = var2[var4];
            resultSb.append(byteToHexString(b));
        }

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (b < 0) {
            n = b + 256;
        }

        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static Map<String, Object> generateWxACodeParam(String scene, String page) {
        Map<String, Object> param = new HashMap();
        param.put("width", 430);
        param.put("auto_color", true);
        param.put("scene", scene);
        if (StringUtils.isNotBlank(page)) {
            param.put("page", page);
        }

        return param;
    }

    public static String generateMessageId() {
        return UUID.randomUUID().toString();
    }




}
